Ext.define('archives.controller.User', {
	extend : 'archives.controller.GridController',
			
	models : [ 'User' ],
	stores : [ 'User' ],
	views : [ 'user.Main','user.Edit','user.Password' ],
	mainView : 'user.Main',
	mainEdit : 'user.Edit',
	mainModel: 'User',
	
	initMainView : function()
	{
		this.callParent();
		
		this.getMainView().on('modifyPassword', this.onModifyPassword, this);
		this.getMainView().on('resetPassword', this.onResetPassword, this);
		this.getMainView().on('userActivate', this.onUserActivate, this);
		this.getMainView().on('userDisabled', this.onUserDisabled, this);
	},
	
	onModifyPassword : function()
	{
		var item = this.getGridSingleItem();
		if (item == null) return;
		
		this.winShow('user.Password',null,G_OPERACTION.MODIFY);
	},
	addWinListener : function(winName)  //subClass extend
	{
		this.callParent(arguments);
		
		if ('user.Password' === winName)
		{
			var win = this.winMap.get(winName);
			win.on('save',this.onSavePassword,this);
		}
	},
	
	onSavePassword : function(_this,win)
	{
		var formPanel = win.down('form').getForm();
		
		var oldPassword = win.down('#txtOldPassword').getValue();
		var newPassword = win.down('#txtNewPassword').getValue();
		var confirmNewPassword = win.down('#txtConfirmNewPassword').getValue();
		
		if (!this.passwordFormValid(formPanel,newPassword,confirmNewPassword)) return;
		
		newPassword = hex_md5(newPassword);
		oldPassword = hex_md5(oldPassword);
		var item = this.getGridSingleItem();
		var jsonData = {userId:item.data.userId,newPassword:newPassword,oldPassword:oldPassword}
		
		var url = G_SMPACKAGE +'modifyPassword_'+AlUtil.getShortValue(this.mainModel)+'.action';
		AlAjax.postJsonString(url,jsonData,function(response,options, eOpts){eOpts.body.close();},this,win);
		
		return true;
	},

	onResetPassword : function(_this,win)
	{
		var items = this.getGridItems();
		if (items == null) return;
		
		var ids = this.getIdList(items);
		
		var url = G_SMPACKAGE +'resetPassword_'+AlUtil.getShortValue(this.mainModel)+'.action';
		AlAjax.postJsonString(url,ids,null,this,win);
	},
	onUserActivate : function(_this,win)
	{
		var items = this.getGridItems();
		if (items == null) return;
		
		var ids = this.getIdList(items);
		
		var url = G_SMPACKAGE +'activate_'+AlUtil.getShortValue(this.mainModel)+'.action';
		AlAjax.postJsonString(url,ids,this.loadViewData,this,win);
	},
	onUserDisabled : function(_this,win)
	{
		var items = this.getGridItems();
		if (items == null) return;
		
		var ids = this.getIdList(items);
		
		var url = G_SMPACKAGE +'disabled_'+AlUtil.getShortValue(this.mainModel)+'.action';
		AlAjax.postJsonString(url,ids,this.loadViewData,this,win);
	},
	getIdList : function(items)
	{
		var ids = [];
		Ext.each(items, function(item) {
	    		    ids.push(item.data.userId);
	    		});
	    return ids;
	},
	passwordFormValid : function(formPanel,newPassword,confirmNewPassword)
	{
		var result = formPanel.isValid();
		if (!result)
		{
			AlUtil.showMsg(_('form errors in data!'));
	    	return false;
		}
		
		if (newPassword != confirmNewPassword)
		{
			AlUtil.showMsg(_('Two input password does not match, please enter again!'));
			return false;
		}
		
		return true;
	}
});